package com.apexedu.biz.service.impl;

import com.apexedu.biz.Const;
import com.apexedu.biz.action.ServiceCodeSortAction;
import com.apexedu.biz.entity.TServiceCodesort;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.Pager;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.sys.SysConstants;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统字典类别维护
 * 
 * @author Linyang datetime 2009-8-1
 */
@Service("serviceCodeSortService")
public class ServiceCodeSortServiceImpl extends AbstractBaseServiceSupport {

	public List<?> list(Object myaction, Pager pager) throws Exception {
		ServiceCodeSortAction action = (ServiceCodeSortAction) myaction;
		TServiceCodesort tserviceCodeSort = action.getTserviceCodesort();
		String hql = " from TServiceCodesort where 1=1 ";
		if (tserviceCodeSort != null) {
			if (tserviceCodeSort.getZdlb() != null && !"".equals(tserviceCodeSort.getZdlb())) {
				hql += " and zdlb ='" + tserviceCodeSort.getZdlb() + "'";
			}
		}
		long c = getBaseDao().count(" select count(*) as c " + hql);
		pager.setTotalRows(c);
		hql = hql + " order by zdlb";
		List<?> list = getBaseDao().findPageByHql(hql, pager);
		return list;
	}

	public void load(Object myaction) throws Exception {
		ServiceCodeSortAction action = (ServiceCodeSortAction) myaction;
		String wid = action.getWid();
		TServiceCodesort tserviceCodeSort = (TServiceCodesort) getBaseDao().findById(TServiceCodesort.class, wid);

		action.setTserviceCodesort(tserviceCodeSort);
	}

	public void saveOrUpdate(Object myaction) throws Exception {
		ServiceCodeSortAction action = (ServiceCodeSortAction) myaction;
		TServiceCodesort bean = action.getTserviceCodesort();
		String newzdlb = action.getParameter("tserviceCodesort.zdlb");
		if (bean == null) {
			action.addActionError(SysConstants.MESSAGE_SAVE_FAILURE + "[ 被保存的对象为null ]");
		} else {
			if (StringUtil.isBlank(newzdlb)) {
				throw new Exception("请输入字典类别编码！");
			}

			if (StringUtil.isBlank(action.getWid())) {
				TServiceCodesort sort = (TServiceCodesort) getBaseDao().findById(TServiceCodesort.class, newzdlb);
				if (sort != null) {
					throw new Exception("您要保存的类别编码已经被【" + sort.getLbmc() + "】使用！");
				}
				bean.setZdlb(newzdlb);
				getBaseDao().save(bean);
				BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_SERVICE_CODE_SORT,
						SysConstants.LOG_ACTION_SAVE, IdLoginAction.getIpAddr(action.getRequest()));
			} else {
				String oldzdlb = action.getWid();
				if (!newzdlb.equals(bean.getZdlb())) {
					// 类别编码被修改了，要检查新编码是否已经存在于数据中
					TServiceCodesort sort = (TServiceCodesort) getBaseDao().findById(TServiceCodesort.class, newzdlb);
					if (sort != null) {
						throw new Exception("您要保存的类别编码已经被【" + sort.getLbmc() + "】使用！");
					}
				}
				getBaseDao().executeHql("update TServiceCode set zdlb=? where zdlb=?", newzdlb, oldzdlb);
				getBaseDao().executeHql("update TServiceCodesort set zdlb=?,lbmc=? where zdlb=?", newzdlb, bean.getLbmc(),	oldzdlb);
				BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_SERVICE_CODE_SORT,
						SysConstants.LOG_ACTION_UPDATE, IdLoginAction.getIpAddr(action.getRequest()));
			}
		}

	}

	public boolean remove(Object myaction) throws Exception {
		ServiceCodeSortAction action = (ServiceCodeSortAction) myaction;
		String ids = action.getWid();
		getBaseDao().deleteAll("TSysCode", "zdlb", "=", ids);
		boolean deleteSuccess = getBaseDao().deleteAll("TServiceCodesort", "zdlb", "=", ids);
		if (deleteSuccess) {
			BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_SERVICE_CODE_SORT, SysConstants.LOG_ACTION_DEL,
                    IdLoginAction.getIpAddr(action.getRequest()));
		}
		return deleteSuccess;
	}
}
